<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*,javax.servlet.*" %>
<%
    Boolean isLoggedIn = (Boolean) session.getAttribute("isLoggedIn");
    if (isLoggedIn == null || !isLoggedIn) {
        response.sendRedirect("login.jsp"); // 如果用户未登录，重定向到登录页面
        return;
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻管理 - 新闻发布系统</title>
    <link rel="stylesheet" href="css/style.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        .manage-container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 2rem;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .manage-container h2 {
            color: #2c3e50;
            margin-bottom: 2rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid #3498db;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logout-btn {
            padding: 0.5rem 1rem;
            background-color: #e74c3c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            font-size: 0.9rem;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        .news-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }

        .news-table th,
        .news-table td {
            padding: 1rem;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        .news-table th {
            background-color: #f8f9fa;
            color: #2c3e50;
            font-weight: 500;
        }

        .news-table tr:hover {
            background-color: #f8f9fa;
        }

        .action-btn {
            padding: 0.5rem 1rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            font-size: 0.9rem;
            margin-right: 0.5rem;
        }

        .edit-btn {
            background-color: #3498db;
            color: white;
        }

        .edit-btn:hover {
            background-color: #2980b9;
        }

        .delete-btn {
            background-color: #e74c3c;
            color: white;
        }

        .delete-btn:hover {
            background-color: #c0392b;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            position: relative;
            background-color: #fff;
            margin: 10% auto;
            padding: 2rem;
            width: 80%;
            max-width: 600px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .close-btn {
            position: absolute;
            top: 1rem;
            right: 1rem;
            font-size: 1.5rem;
            cursor: pointer;
            color: #666;
        }

        .close-btn:hover {
            color: #333;
        }

        .modal-title {
            margin-bottom: 1.5rem;
            color: #2c3e50;
        }

        .modal-form .form-group {
            margin-bottom: 1.5rem;
        }

        .modal-form label {
            display: block;
            margin-bottom: 0.5rem;
            color: #2c3e50;
        }

        .modal-form input[type="text"],
        .modal-form textarea {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }

        .modal-form textarea {
            min-height: 200px;
            resize: vertical;
        }

        .modal-form .submit-btn {
            padding: 0.8rem 1.5rem;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .modal-form .submit-btn:hover {
            background-color: #2980b9;
        }
    </style>
</head>
<body>
    <header>
        <nav class="navbar">
            <div class="logo">
                <h1>新闻发布系统</h1>
            </div>
            <ul class="nav-links">
                <li><a href="index.jsp"><i class="fas fa-newspaper"></i> 查看新闻</a></li>
                <li><a href="publish.jsp"><i class="fas fa-edit"></i> 发布新闻</a></li>
                <li><a href="manage.jsp" class="active"><i class="fas fa-cog"></i> 新闻管理</a></li>
                <li><a href="login.jsp"><i class="fas fa-user"></i> 管理员登录</a></li>
            </ul>
        </nav>
    </header>

    <main>
        <div class="manage-container">
            <h2>
                新闻管理
                <a href="#" class="logout-btn" id="logoutBtn">
                    <i class="fas fa-sign-out-alt"></i> 管理员退出
                </a>
            <%-- 设计点击事件处理器 --%>
                <script>
                    document.getElementById('logoutBtn').addEventListener('click', function(e) {
                        e.preventDefault();
                        fetch('logout', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        })
                            .then(response => {
                                if (response.ok) {
                                    window.location.href = 'login.jsp'; // 重定向到登录页面
                                } else {
                                    throw new Error('Network response was not ok.');
                                }
                            })
                            .catch(error => {
                                console.error('There has been a problem with your fetch operation:', error);
                            });
                    });
                </script>
            </h2>
            <table class="news-table">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="newsTableBody">
                    <!-- 新闻列表将通过JavaScript动态加载 -->
                </tbody>
            </table>
        </div>
    </main>

    <!-- 编辑新闻模态框 -->
    <div class="modal" id="editModal">
        <div class="modal-content">
            <span class="close-btn" id="closeModal">&times;</span>
            <h3 class="modal-title">编辑新闻</h3>
            <form class="modal-form" id="editForm">
                <input type="hidden" id="editNewsId">
                <div class="form-group">
                    <label for="editTitle">新闻标题</label>
                    <input type="text" id="editTitle" name="title" required>
                </div>
                <div class="form-group">
                    <label for="editContent">新闻内容</label>
                    <textarea id="editContent" name="content" required></textarea>
                </div>
                <button type="submit" class="submit-btn">保存修改</button>
            </form>
        </div>
    </div>

    <footer>
        <p>&copy; 2024 新闻发布系统 版权所有</p>
    </footer>
</body>
</html> 